<template>
  <el-dialog :title="typeName" :visible="showDialog" :before-close="close">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px" :label-position="labelPosition">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%;" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%;" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="form.managerId" placeholder="请选择活动区域" style="width: 80%;">
          <el-option v-for="item in manangerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" placeholder="1-100个字符" style="width: 80%;" />
      </el-form-item>
      <el-form-item class="dialog-footer">
        <el-button type="primary" @click="onSubmit">确认</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getdepartment, getManangerInfo, addDepartment, editDepartment, updateDepartment } from '@/api/department'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    },
    typeName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      labelPosition: 'right',
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称应该为2-10位之间', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let res = await getdepartment() // 获取所有部门数据
              if (this.form.id) { // 如果是编辑功能,验证时排除自身
                res = res.filter(item => item.id !== this.form.id)
              }
              if (res.some(item => item.name === value)) { // 判断是否重复,只要有一个重复就触发
                callback(new Error('部门名称重复')) // 提示错误
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码应该为2-10位之间', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let res = await getdepartment()
              if (this.form.id) { // 如果是编辑功能,验证时排除自身
                res = res.filter(item => item.id !== this.form.id)
              }
              if (res.some(item => item.code === value)) {
                callback(new Error('部门名称重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        resperson: [
          { required: true, message: '请输入部门负责人', trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍应该为1-100位之间', trigger: 'blur' }
        ]
      },
      manangerList: []
    }
  },
  created() {
    this.getManagerData()
  },
  methods: {
    close() {
      this.form = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async onSubmit() {
      await this.$refs.form.validate()
      // 发送请求
      if (this.typeName === '编辑部门') {
        await updateDepartment(this.form)
        this.$message.success('修改部门成功')
      } else {
        await addDepartment({ ...this.form, pid: this.currentId })
        this.$message.success('新增部门成功')
      }
      // 通知父组件,更新数据,重新渲
      this.$emit('updataData')
      this.close()
    },
    async getManagerData() {
      const res = await getManangerInfo()
      this.manangerList = res
    },
    async editDepartmentInfo() { // 编辑功能
      const res = await editDepartment(this.currentId)
      this.form = res
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog-footer {
  margin: 0 auto;
}
</style>
